package org.eclipseuseradmin.ui.wizards;

import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.DecoratingLabelProvider;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.PlatformUI;
import org.eclipseuseradmin.internal.ui.RoleRootsRegistry;
import org.eclipseuseradmin.internal.ui.RolesContentProvider;
import org.eclipseuseradmin.internal.ui.RolesLabelProvider;
import org.eclipseuseradmin.internal.ui.UserAdminUiPlugin;
import org.eclipseuseradmin.internal.ui.wizards.AddRoleElementWizard;
import org.eclipseuseradmin.internal.ui.wizards.WizardsMessages;
import org.eclipseuseradmin.ui.BaseRoleElementAdapter;
import org.eclipseuseradmin.ui.IRoleElement;
import org.eclipseuseradmin.ui.IRoleRootDescriptor;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdmin;

public class AddRoleElementWizardPage extends WizardPage {

	private TreeViewer viewer;

	public AddRoleElementWizardPage() {

		super("main", WizardsMessages.get().AddRoleElementWizardPage_title,
				null);
		setDescription(WizardsMessages.get().AddRoleElementWizardPage_description);
	}

	public void createControl(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);

		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		composite.setLayout(layout);

		createViewer(composite);

		setControl(composite);
	}

	private void createViewer(Composite parent) {

		viewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI);
		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
		viewer.getControl().setLayoutData(layoutData);

		viewer.setContentProvider(getRolesContentProvider());
		ILabelDecorator decorator = PlatformUI.getWorkbench()
				.getDecoratorManager().getLabelDecorator();
		viewer.setLabelProvider(new DecoratingLabelProvider(
				getRolesLabelProvider(), decorator));
		viewer.addFilter(getViewerFilter());
		viewer.setInput(getTreeRoot());
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {

				getAddWizard().setWizardRoleSelection(
						(IStructuredSelection) event.getSelection());
			}
		});
	}

	private RoleRootsRegistry getTreeRoot() {

		return getRoleRootsRegistry();
	}

	private RolesContentProvider getRolesContentProvider() {

		RolesContentProvider provider = new RolesContentProvider(viewer);
		provider.setUserAdmin(getUserAdmin());
		provider.setAdapterManager(getAdapterManager());
		return provider;
	}

	private RolesLabelProvider getRolesLabelProvider() {

		RolesLabelProvider provider = new RolesLabelProvider();
		provider.setEditorRegistry(getEditorRegistry());
		provider.setAdapterManager(getAdapterManager());
		return provider;
	}

	private RolesFilter getViewerFilter() {

		RolesFilter filter = new RolesFilter();
		filter.setRoleElement(BaseRoleElementAdapter
				.adapt((Role) getAddWizard().getSelection().getFirstElement()));
		return filter;
	}

	private UserAdmin getUserAdmin() {
		return UserAdminUiPlugin.getUserAdmin();
	}

	private IAdapterManager getAdapterManager() {
		return Platform.getAdapterManager();
	}

	private RoleRootsRegistry getRoleRootsRegistry() {
		return UserAdminUiPlugin.getRoleRootsRegistry();
	}

	private IEditorRegistry getEditorRegistry() {
		return PlatformUI.getWorkbench().getEditorRegistry();
	}

	public AddRoleElementWizard getAddWizard() {
		return (AddRoleElementWizard) getWizard();
	}

	public TreeViewer getViewer() {
		return viewer;
	}

	private class RolesFilter extends ViewerFilter {

		IRoleElement roleElement;

		@Override
		public boolean select(Viewer viewer, Object parentElement,
				Object element) {

			if (element instanceof IRoleRootDescriptor) {
				return true;
			}
			if (element instanceof Role) {
				return roleElement.acceptChild((Role) element);
			}
			return false;
		}

		public IRoleElement getRoleElement() {
			return roleElement;
		}

		public void setRoleElement(IRoleElement roleElement) {
			this.roleElement = roleElement;
		}

	}

}
